<template>
  <el-card class="box-card">
    <template #header>
      <el-form :inline="true" :model="formInline">
        <el-form-item label="货物编码">
          <el-input v-model="formInline.cargoSn" placeholder="请输入货物编码" clearable />
        </el-form-item>
        <el-form-item label="货物名称">
          <el-input v-model="formInline.cargoName" placeholder="请输入货物名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button type="primary" @click="dialogVisible=true" plain>新增</el-button>
          <el-button @click="init">重置</el-button>
        </el-form-item>
      </el-form>
    </template>

    <el-table :data="tableParams.rows">
      <el-table-column prop="cargoSn" label="货物编码" />
      <el-table-column prop="cargoName" label="货物名称" />
      <el-table-column prop="cargoSpec" label="规格" />
      <el-table-column label="单位">
        <template #default="scope">
          <selectUnit v-model="scope.row.cargoUnit" readonly="1"></selectUnit>
        </template>
      </el-table-column>
      <el-table-column prop="storeNum" label="库存数量" />
      <el-table-column prop="num" label="计划调拨数" />
      <el-table-column prop="realNum" label="实际调拨数" />
      <el-table-column label="操作" fixed="right" width="180" align="center">
        <template #default="scope">
          <el-button type="primary" text @click="handClick(scope.row,1)">修改</el-button>
          <el-button type="primary" text @click="handClick(scope.row,2)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-if="tableParams['total']" :total="tableParams['total']" v-model:page="formInline.pageNum" v-model:limit="formInline.pageSize" @pagination="getInfo" />

    <el-dialog v-model="dialogVisible" title="新增/编辑货物" width="800" :before-close="handleClose">
      <el-form :model="form" label-width="100" ref="formEl">
        <el-form-item label="货物编码">
          <div class="cargoSn" style="width:100%" @click="innerVisible=true">
            <el-input v-model="form.cargoSn" placeholder="请选择货物名称" />
          </div>
        </el-form-item>
        <el-form-item label="货物名称" prop="cargoName">
          <el-input placeholder="请输入货物名称" clearable disabled v-model="form.cargoName" />
        </el-form-item>
        <el-form-item label="规格">
          <el-input placeholder="请输入规格" clearable disabled v-model="form.cargoSpec" />
        </el-form-item>
        <el-form-item label="单位">
          <selectUnit v-model="form.cargoUnit" disabled></selectUnit>
        </el-form-item>
        <el-form-item label="库存数量">
          <el-input placeholder="请输入单位" clearable disabled v-model="form.storeNum" />
        </el-form-item>
        <el-form-item label="计划挑拨数" prop="num">
          <el-input-number v-model="form.num" :min="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            确 认
          </el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="innerVisible" width="850" title="货物列表" append-to-body>
      <el-table :data="cargos" @row-click="cargoInfo">
        <el-table-column prop="cargoName" label="货物" fixed="left" />
        <!-- <el-table-column prop="batchNumber" label="批次号" width="150" /> -->
        <el-table-column prop="cargoSn" label="货物编码" width="180" />
        <el-table-column prop="cargoSpec" label="规格" />
        <el-table-column prop="cargoUnit" label="单位">
          <template #default="scope">
            <selectUnit v-model="scope.row.cargoUnit" readonly="1"></selectUnit>
          </template>
        </el-table-column>
        <el-table-column prop="factoryName" label="工厂" />
        <el-table-column prop="storeName" label="仓库" />
        <el-table-column prop="storeRegionName" label="库区" />
        <el-table-column prop="storeRegionLocationName" label="库位" width="180" />
        <el-table-column prop="trayName" label="托盘" width="150" />
        <el-table-column prop="totalNum" label="库存" width="80" fixed="right" align="center" />
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted } from "vue";
import { useRoute } from "vue-router";
const route = useRoute();
const { proxy } = getCurrentInstance();
let tableParams = ref({});
let dialogVisible = ref(false);
let innerVisible = ref(false);
let formInline = ref({
  pageNum: 1,
  pageSize: 10,
  cargoSn: "",
  cargoName: "",
  transferId: "",
});
let loading = ref(false);
let transferId = null;
let form = ref({
  transferId: "",
  cargoId: "",
  cargoSn: "",
  cargoName: "",
  cargoUnit: "",
  cargoSpec: "",
  num: 1,
  storeNum: null,
});
let query = {
  outFactoryId: null,
  outStoreId: null,
};
let cargos = ref([]);
let cargoData = ref({});
let cargoTotalNum = ref(0); //商品库存

onMounted(() => {
  transferId = route.query.id;
  query.outFactoryId = route.query.outFactoryId;
  query.outStoreId = route.query.outStoreId;
  search();
});

let search = () => {
  formInline.value.pageNum = 1;
  getInfo();
};
let getInfo = async () => {
  formInline.value.transferId = transferId;
  tableParams.value = await proxy.$apis.transferDetailList(formInline.value);
  let { data } = await proxy.$apis.queryTransferList(query);
  let list = [];
  data.forEach((element) => {
    let index = hasCargos(element, list);
    if (index == null) {
      list.push(element);
    } else {
      list[index].totalNum += element.totalNum;
    }
  });
  cargos.value = list;
  console.log("查询货物", cargos.value);
};
//判断有没有这个货物
function hasCargos(item, list) {
  let current = null;
  list.forEach((element, index) => {
    if (
      element.cargoId == item.cargoId &&
      element.factoryId == item.factoryId &&
      element.storeId == item.storeId &&
      element.storeRegionId == item.storeRegionId &&
      element.storeRegionLocationId == item.storeRegionLocationId
    )
      current = index;
  });
  return current;
}
let handleClose = () => {
  form.value = {
    transferId: "",
    cargoId: "",
    cargoSn: "",
    cargoName: "",
    cargoUnit: "",
    cargoSpec: "",
    num: 1,
    storeNum: null,
  };
  proxy.$refs.formEl.resetFields();
  getInfo();
  dialogVisible.value = false;
};
let init = () => {
  formInline.value = {
    pageNum: 1,
    pageSize: 10,
    cargoSn: "",
    cargoName: "",
    transferId: "",
  };
  getInfo();
};
let cargoInfo = (e, column, event) => {
  form.value.cargoId = e.cargoId;
  form.value.cargoSn = e.cargoSn;
  form.value.cargoName = e.cargoName;
  form.value.cargoUnit = e.cargoUnit;
  form.value.cargoSpec = e.cargoSpec;
  form.value.storeNum = e.totalNum;
  innerVisible.value = false;
};
let handClick = async (row, num) => {
  switch (num) {
    case 1:
      form.value = proxy.$utils.formatFunction(row, { ...form.value });
      form.value.id = row.id;
      dialogVisible.value = true;
      break;
    case 2:
      let resp = await proxy.$utils.confirmTips("是否确定删除?");
      if (!resp) return;
      await proxy.$apis.transferDetailDel({ id: row.id });
      proxy.$utils.showToast("删除成功");
      formInline.value.pageNum = 1;
      getInfo();
      break;
    default:
      break;
  }
};
let submitForm = async () => {
  await proxy.$refs.formEl.validate((valid, fields) => {
    if (valid) {
      save();
    } else {
      for (let key in fields) {
        return proxy.$utils.showToast(fields[key][0].message, 0);
      }
    }
  });
};
let save = async () => {
  try {
    loading.value = true;
    if (form.value.id) {
      await proxy.$apis.transferDetailEdit(form.value);
    } else {
      let { data } = await proxy.$apis.transferDetailQuery({
        cargoSn: form.value.cargoSn,
      });
      if (data.length) {
        return proxy.$utils.showToast(
          `已添加过货物 - ${form.value.cargoName}`,
          0
        );
      }
      form.value.transferId = transferId;
      await proxy.$apis.transferDetailAdd(form.value);
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.box-card {
  margin: 20px;
}
</style>
